package com.example.battle.config;

import com.example.battle.handler.MyHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.handler.TextWebSocketHandler;

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    /**
     * 提供配置自己的websocekt类即请求路径
     */
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        //registry.addHandler(myHandler(),"/dun/dunHandler").addInterceptors(new WebSocketInterceptor());
        registry.addHandler(webSocketHandler(),"/socketServer")
                .addInterceptors(new WebSocketInterceptor()).setAllowedOrigins("*");
    }

    /**
     * 向spring容器注册javabean由spring容器来管理
     * @return
     */
    @Bean
    public TextWebSocketHandler webSocketHandler(){
        return new MyHandler();
    }
}
